﻿namespace CloundNinja.Metering.RowCountsProvider.Configuration
{
    using System;
    using System.Configuration;
    using System.Linq;

    using CloudNinja.Metering.Common;
    using CloudNinja.Metering.Common.Configuration;
    using CloudNinja.Metering.Data;
    using CloudNinja.Metering.Providers;

    using Microsoft.Practices.Unity;

    public class TenantRowCountProviderSettingsElement : ProviderElement
    {
        [ConfigurationProperty("TenantRowCountMeteringProviderSettings")]
        public TenantRowCountMeteringProviderSettings TenantRowCountProviderMeteringProviderSettings
        {
            get
            {
                return this["TenantRowCountMeteringProviderSettings"] as TenantRowCountMeteringProviderSettings;
            }

            set
            {
                this["TenantRowCountMeteringProviderSettings"] = value;
            }
        }

        public override void RegisterType(IUnityContainer container, MeteringSection meteringConfiguration)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            var connections = from object account in
                                  this.TenantRowCountProviderMeteringProviderSettings.StorageAccounts
                              select account as TenantRowCountConnectionCollectionElement;

            var connectionsList = from connection in connections
                                  select
                                      new TenantRowCountSpec
                                          {
                                              ServerAlias = connection.ServerAlias,
                                              ConnectionString = connection.ConnectionString,
                                              TableName = connection.TableName,
                                              TenantIdColumn = connection.TenantIdColumn,
                                              CountColumn = connection.CountColumn,
                                              TenantIdPattern = connection.TenantIdPattern
                                          };

            var rowCountsProviderInjectionConstructor = new InjectionConstructor(
                this.CollectInterval,
                container.Resolve<IMeteringRepository>(),
                container.Resolve<IMeteringContext>(),
                connectionsList);

            container.RegisterType<IMeteringProvider, RowCountsProvider>(
                typeof(RowCountsProvider).ToString(),
                new ContainerControlledLifetimeManager(),
                rowCountsProviderInjectionConstructor);
        }

        protected override object OnRequiredPropertyNotFound(string name)
        {
            // HACK: Do not really understand why it comes here with these properties, which is clearly defined above.
            return name.Equals("TenantRowCountMeteringProviderSettings") ? null : base.OnRequiredPropertyNotFound(name);
        }
    }
}
